﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleClient
{
    public partial class Form1 : Form
    {

        private mooseirc.irc.IrcClient client;

        public Form1()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            client = new mooseirc.irc.IrcClient();
            client.StandardMessage += onOutputMessage;
            return;
        }

        private void txtInput_TextChanged(object sender, EventArgs e) {

        }

        private void onOutputMessage(string message) {
            rtxtOutput.Text += "\r\n";
            rtxtOutput.Text += message;
            return;
        }

        private void txtInput_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == '\r') {
                Execute(txtInput.Text);
                txtInput.Clear();
                e.Handled = true;
            }
            return;
        }

        private void Execute(string input) {
            string cmd;
            string[] parms = null;
            if (input[0] == '/') {
                if (input.Trim().IndexOf(' ') == -1) {
                    cmd = input.Substring(1).Trim();
                }
                else {
                    cmd = input.Substring(1, input.IndexOf(' ') - 1);
                    parms = input.Substring(input.IndexOf(' ')).Split(new string[] { " ", ",", ":", "|" }, StringSplitOptions.RemoveEmptyEntries);

                }


                switch (cmd) {
                    case "server":
                        client.connect(parms[0], int.Parse(parms[1]));
                        break;
                    case "disconnect":
                        client.disconnect();
                        break;
                    case "nick":
                        client.nick("mikeyj");
                        break;
                    case "user":
                        client.user("mikeyj", "", "", "mike mike");
                        break;
                    default:
                        client.send_raw(input.Substring(1) + "\r\n");
                        break;
                }

            }

            return;
        }

    }
}
